//
//  Son+Eat.m
//  runtime的两个方法替换
//
//  Created by 中创 on 2020/1/13.
//  Copyright © 2020 LS. All rights reserved.
//

#import "Son+Eat.h"

#import <objc/runtime.h>

@implementation Son (Eat)

+ (void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        Method method1 = class_getInstanceMethod(self, @selector(eat));
        Method method2 = class_getInstanceMethod(self, @selector(anotherEat));
        
        BOOL didAddMethod = class_addMethod(self, @selector(eat), method_getImplementation(method2), method_getTypeEncoding(method2));
        if (didAddMethod) {
            class_replaceMethod(self,
            @selector(anotherEat),
            method_getImplementation(method1),
            method_getTypeEncoding(method1));
        }else{
            method_exchangeImplementations(method1, method2);
        }

    });
}

- (void)anotherEat{
    NSLog(@"self:%@", self);
    [self anotherEat];
    NSLog(@"替换之后的吃的方法...");
}

//- (void)eat{
//    NSLog(@"儿子吃东西...");
//}

- (void)eat1{
    NSLog(@"%s", __func__);
}



@end
